from selenium import webdriver
from ElementsClass import xpathInHead

#This example requires Selenium WebDriver 3.13 or newer
# with webdriver.Firefox() as driver:
    # wait = WebDriverWait(driver, 10)
    # driver.get("https://google.com/ncr")
    # driver.find_element_by_name("q").send_keys("cheese" + Keys.RETURN)
    # first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
    # print(first_result.get_attribute("textContent"))
class MyDriver(object):
    '''封装webdriver为内部使用，确保内部调用的一致性'''
    # 构造函数
    def __init__(self,):
        self.page = webdriver.Chrome()
    #导航到登录页
    def goto_chandao_login(self,):
        #设置全局等待时间,每当查找一个元素的时候等待的最长时间
        self.page.implicitly_wait(30)
        self.page.maximize_window()
        self.page.get("https://demo.zentao.net/")
        #等待页面完全加载
        element = self.page.find_element_by_xpath(xpathInHead.易软天创.value)
        print(element.text)
        # demo网站特有选项
        self.page.find_element_by_xpath('//*[@id="userNav"]/li/a').click()

        self.page.find_element_by_xpath('//*[@id="userNav"]/li/ul/li[7]/a').click()

        print(self.page.current_url)


    def login(self,selectRole,expectInfo):
        #开始登录
        #选择角色
        self.page.find_element_by_xpath(selectRole).click()
        #登陆
        self.page.find_element_by_xpath('//*[@id="submit"]').click()
        #查看当前角色
        afterLogin = self.page.find_element_by_xpath('//*[@id="userNav"]/li/a/span[1]').text
        #判断角色是否为预期
        result = expectInfo == afterLogin
        self.logout()
        return result
        
    def logout(self):
        self.page.find_element_by_xpath(xpathInHead.用户.value).click()
        self.page.find_element_by_xpath(xpathInHead.退出.value).click()
        
    def ddt_test(self,dict_data):
        results = []
        if isinstance(dict_data,dict):
            for key in dict_data:
                results.append(self.login(key,dict_data[key]))
        else:
            print('wrong data')
        return results
    def quit(self):
        self.page.quit()
    page=None